//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

// Objects     : Three functions used in bsp (kernel):
//                   KrnSetup, KrnInitialize, KrnStartup;
// Type        : Functional Test;
// Prototype   :
//     DECL_CODEINIT void CDECL TestKernelSetup0(physaddr_t, physaddr_t);
//         Parameters :
//             [in] physaddr_t paKernelPhysBase: PA of kernel;
//             [in] physaddr_t paKernelImagePhysBase: PA of kernel image;
//     DECL_CODEINIT void CDECL TestKernelInitialize0();
//     DECL_CODEINIT void CDECL TestKernelStartup0();
// Description : Three test functions are utilized to verify the status of the
//                   kernel when bsp is initializing. Since File I/O is not
//                   utilized here, I just print the status of the kernel to
//                   the screen and COM2 serial port.
//               TestKernelSetup0: A test function, which is invoked when
//                   KrnSetup is called.
//               TestKernelInitialize0: A test function, which is invoked when
//                   KrnInitialize is called.
//               TestKernelStartup0: A test function, which is invoked when
//                   KrnStartup is called.
//               In TestKernelSetup0, No messages is printed out since paging
//                   disabled and COM2 serial device unavailable.
//               In TestKernelInitialize0, the follow information is printed:
//                   info. about paging(MMU);
//                   info. about segmentation(MMU);
//                   info. about CRT;
//                   info. about boot modules;
//                   info. about boot command line;
//                   info. about memory;
//                   info. about PIC;
//                   info. about PIT;
//                   info. otherwise;
//               In TestKernelStartup0, the follow information is printed:
//                   info. about boot command line;
//                   info. about interrupts;
//                   info. about kernel debuger initialization;
//                   info. about physical memory;
//                   info. about kernel thread 0;
//                   info. about mantle initialization;
//                   info. about kernel state;
// Input File  : None;
// Output File : None;
// Environment : Elastos 2.0 dbg, opt, rls (with environment variable
//                   KERNEL_TESTING set to 1);
// Tester      : Fan Dian;
// Date        : 2004/1;

#include <core.h>
#include "paging.h"
#include "segments.h"
#include "crt.h"
#include "pic.h"
#include "pit.h"
#include "interrupts.h"
//#include "kdb.h"
#include "physmem.h"
#include "kernelstate.h"
#include "kthread0.h"
#include "mantle.h"

EXTERN_C DECL_CODEINIT void CDECL TestKernelSetup0(
    physaddr_t paKernelPhysBase, physaddr_t paKernelImagePhysBase)
{
    // No messages are printed since paging disabled and console unavailable
}

EXTERN_C DECL_CODEINIT void CDECL TestKernelInitialize0()
{
    kprintf("******FUNCTION_BSP_FANDIAN\n");
    kprintf("...... Tester0: TestKernelInitialize Start ......\n");

    // Print info. about paging(MMU)
    TestPaging();

    // Print info. about segmentation(MMU)
    TestSegments();

    // Print info. about CRT
    TestCtors();

    // Print info. about boot modules
    kprintf("Totol: %d Boot Modules, Page [0x%x, 0x%x)\n", \
        bsp.uNumberOfBootModules, \
        bsp.regionOfBootModules.uBasePageNo, \
        bsp.regionOfBootModules.uLimitPageNo);
    for (uint_t i = 0; i < bsp.uNumberOfBootModules; i++) {
        kprintf("    Module [%d]: Name = %s, Page [0x%d, 0x%d)\n", i, \
            bsp.pBootModules[i].szName, \
            bsp.pBootModules[i].region.uBasePageNo, \
            bsp.pBootModules[i].region.uLimitPageNo);
    }

    // Print info. about boot command line
    kprintf("BootCommandLine = %s\n", bsp.szBootCommandLine);

    // Print info. about memory
    TestRegions("InitStack", &bsp.regionOfInitStack, 1);
    TestZones();
    kprintf("KernelPreferredZoneNo = %d\n", bsp.uKernelPreferredZoneNo);
    kprintf("UserPreferredZoneNo = %d\n", bsp.uUserPreferredZoneNo);
    TestHoles();

    // Print info. about console

    // Print info. about PIC
    TestPic();

    // Print info. about PIT
    TestPit();

    // Print info. otherwise
    TestKernelState();
    kprintf("CpuDescription = %s\n", bsp.szCpuDescription);
    kprintf("BoardDescription = %s\n", bsp.szBoardDescription);

    kprintf("...... Tester0: TestKernelInitialize End ......\n");
    kprintf("******FUNCTION_BSP_FANDIAN_END\n");
}

EXTERN_C DECL_CODEINIT void CDECL TestKernelStartup0()
{
    kprintf("******FUNCTION_BSP_FANDIAN\n");
    kprintf("...... Tester0: TestKernelStartup Start ......\n");

    // Print info. about boot command line
    kprintf("BootComandLine = %s\n", bsp.szBootCommandLine);

    // Print info. about interrupts
    TestInterrupts();

    // Print info. about kernel debuger
//    TestKdb();

    // Print info. about physical memory
//    kprintf("c_uMaxNumberOfPages = %d\n", c_uMaxNumberOfPages);
//    kprintf("c_uKernelBasePageNo = %d\n", c_uKernelBasePageNo);
//    kprintf("c_uNumberOfKernelPages = %d\n", c_uNumberOfKernelPages);
//    TestBuddySystem();

    // Print info. about kernel threads
    TestThread0();

    // Print info. about mantle
//    TestMantle();

    // Print info. about kernel state
    TestKernelState();

    kprintf("...... Tester0: TestKernelStartup End ......\n");
    kprintf("******FUNCTION_BSP_FANDIAN_END\n");
}
